package com.caigou.service.impl;

import com.caigou.bean.cms.entity.ArticleBook;
import com.caigou.mapper.cms.ArticleBookMapper;
import com.caigou.pojo.ArticleBookData;
import com.caigou.service.ArticleBookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("articleBookService")
public class ArticleBookServiceImpl implements ArticleBookService {

    @Resource
    private ArticleBookMapper articleBookMapper;

    @Override
    public ArticleBook saveArticleBookData(String articleId, String bookPrice, Integer bookImageId, String bookImage, String bookContent) {
        ArticleBook articleBook = new ArticleBook();
        articleBook.setArticleId(articleId);
        articleBook.setBookPrice(bookPrice);
        articleBook.setBookImageId(bookImageId);
        articleBook.setBookImage(bookImage);
        articleBook.setBookContent(bookContent);
        articleBookMapper.saveArticleBookData(articleBook);
        return articleBook;
    }

    @Override
    public ArticleBookData loadArticleBookData(String articleId) {
        return articleBookMapper.loadArticleBookData(articleId);
    }

    @Override
    public int updateArticleBookData(String articleId, String bookPrice, Integer bookImageId, String bookImage, String bookContent) {
        ArticleBook articleBook = articleBookMapper.loadArticleBook(articleId);
        boolean isCreated = false;
        if (null == articleBook){
            isCreated =true;
            articleBook = new ArticleBook();
        }
        articleBook.setArticleId(articleId);
        articleBook.setBookPrice(bookPrice);
        articleBook.setBookImageId(bookImageId);
        articleBook.setBookImage(bookImage);
        articleBook.setBookContent(bookContent);
        if (isCreated){
            return articleBookMapper.saveArticleBookData(articleBook);
        } else {
            return articleBookMapper.updateArticleBookData(articleBook);
        }
    }

    public int deleteArticleBookByArticleId(String articleId){
        return articleBookMapper.deleteArticleBookByArticleId(articleId);
    }
}
